<template>
	<div>
		<el-table  :data="students" style="width: 100%">
			<el-table-column prop="id" label="学号" width="180">
			</el-table-column>
			<el-table-column prop="name" label="姓名" width="180">
			</el-table-column>
			<el-table-column prop="address" label="地址">
			</el-table-column>
			<el-table-column  label="操作">
				<!-- 作用域插槽 slot -->
				<template slot-scope="scope">	
					<el-button type="warning" @click="doUpdate(scope.row)">编辑</el-button>
					<el-button type="danger" @click="del(scope.row.id)">删除</el-button>
				</template>
				
			</el-table-column>
		</el-table>
		
		<!-- 编辑对话框 -->
		<el-dialog title="收货地址" :visible.sync="dialogFormVisible">
		  <el-form >
		    <el-form-item label="姓名" label-width="100px">
		      <el-input v-model="updateStu.name"></el-input>
		    </el-form-item>
		    <el-form-item label="地址" label-width="100px">
		      <el-input v-model="updateStu.address"></el-input>
		    </el-form-item>
		  </el-form>
		  <div slot="footer" class="dialog-footer">
		    <el-button @click="dialogFormVisible = false">取 消</el-button>
		    <el-button type="primary" @click="update()">确 定</el-button>
		  </div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				dialogFormVisible:false, // dialogFormVisible变量来控制对话框的显示和隐藏
				students: [{
					id: '1001',
					name: '王小虎',
					address: '上海市普陀区金沙江路 1518 弄'
				}, {
					id: '1002',
					name: '孙悟空',
					address: '上海市普陀区金沙江路 1517 弄'
				}, {
					id: '1003',
					name: '猪八戒',
					address: '上海市普陀区金沙江路 1519 弄'
				}, {
					id: '1004',
					name: '白骨精',
					address: '上海市普陀区金沙江路 1516 弄'
				}],
				updateStu:{id:'',name:'',address:''}  //修改的数据
			}
		},
		//放函数
		methods:{
			// 回显数据
			doUpdate(row){
				console.log(row);
				this.dialogFormVisible = true //先显示对话框
				//数据回显
				this.students.map((item) => {
					if(row.id == item.id){
						this.updateStu.id = row.id
						this.updateStu.name = row.name
						this.updateStu.address = row.address
					}
					
				})
				
			},
			//修改
			update(){
				
				let arr = this.students.filter((item)=>{
					//先判断是否是更改的数据
					if(this.updateStu.id == item.id){
						//item.id  = this.updateStu.id
						item.name = this.updateStu.name
						item.address = this.updateStu.address
						return item;
					}
				})
				this.students = arr
				this.dialogFormVisible = false
			},
			//删除
			del(id){
				console.log(id);
				let arr = this.students.filter(item => {
					return item.id!=id
				})
				this.students = arr
			}
			
		}
		
		
	}
</script>

<style>
</style>